package com.ssii.www.myproject.web.controller;

import com.ssii.www.myproject.common.util.AjaxResult;
import com.ssii.www.myproject.common.util.AjaxResultB;
import com.ssii.www.myproject.common.util.PageList;
import com.ssii.www.myproject.common.util.UserContext;
import com.ssii.www.myproject.domain.Task;
import com.ssii.www.myproject.query.BaseQuery;
import com.ssii.www.myproject.query.TaskQuery;
import com.ssii.www.myproject.service.TaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@Controller
@RequestMapping("/task")
public class TaskController extends BaseController {
  @Autowired
  private TaskService taskService;

  @RequestMapping("/index")
  @RequiresPermissions("task:index")
  public String index() {
    return "task/index";
  }

//  @RequestMapping("/page")
//  @ResponseBody
//  public PageList<Task> page(TaskQuery query) {
//    if(query.getQ()!=null){
//      //查询框查询申请人
//      query.setApplicant(query.getQ());
//    }
//    Page<Task> pageByQuery = taskService.findPageByQuery(query);
//    return new PageList(pageByQuery);
//  }

  @RequestMapping("/page")
  @ResponseBody
  public Map<String, Object> page(TaskQuery query) {
    Map<String, Object> map = new HashMap<>();
    if (query.getQ() != null) {
      //查询框查询申请人
      query.setApplicant(query.getQ());
    }
    query.setPageSize(query.getRows());
    query.setCurrentPage(query.getPage());
    Page<Task> pageByQuery = taskService.findPageByQuery(query);
    map.put("rows", pageByQuery.getContent());
    map.put("page", pageByQuery.getTotalPages());
    map.put("total", pageByQuery.getTotalElements());
    return map;
  }

  @RequestMapping("/unadopt")
  @ResponseBody
  //不同意
  public AjaxResult submit(Task task) {
    if (task.getState() != 1) {
      return new AjaxResult("请选择未处理的任务");
    }
    task.setAssignee(UserContext.getUser().getName());//设置办理人
    task.setState(3L);//设置状态为拒绝
    task.setLicense("无");
    taskService.save(task);
    return new AjaxResult();
  }

  @ResponseBody
  @RequestMapping("/adopt")
  public AjaxResult confirm(Task task) {
    if (task.getState() != 1) {
      return new AjaxResult("请选择未处理的任务");
    }
    List<Task> tasks = taskService.findTasksByApplyCode(task.getApplycode());
    tasks.stream().filter(task1 -> tasks.size() > 1).filter(task1 -> task1.getState() == 2).forEach(d -> {
      if (d.getState() == 2L) {
        d.setState(4L);
        taskService.save(d);
      }
    });

//    if (tasks.size() > 1) {
//      for (Task task1 : tasks) {
//        //只存在一条数据通过申请
//        if (task1.getState() == 2) {
//          task1.setState(4L);
//          taskService.save(task1);
//        }
//      }
//    }
    task.setAssignee(UserContext.getUser().getName());//设置办理人
    task.setState(2L);
    task.setDenyrea("没有拒绝原因");
    taskService.save(task);
    return new AjaxResult();
  }

}
